package com.mobileservice.autoreply.data.manager;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.j256.ormlite.stmt.Where;
import com.mobileservice.autoreply.data.database.DatabaseDefination.PhoneContactTable;
import com.mobileservice.autoreply.data.database.DatabaseHelper;
import com.mobileservice.autoreply.ui.model.PhoneContactModel;

public class PhoneContactManager {

	private static PhoneContactManager INSTANCE;
	private static DatabaseHelper databaseHelper;

	private PhoneContactManager(Context context) {
		databaseHelper = new DatabaseHelper(context);
	}

	public static PhoneContactManager getInstance(Context context) {
		if (INSTANCE == null || databaseHelper == null) {
			INSTANCE = new PhoneContactManager(context);
		}
		return INSTANCE;
	}

	public boolean InsertorUpdatePhoneContacts(Cursor cursor) {
		while (cursor.moveToNext()) {
			String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

			String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

			PhoneContactModel model = new PhoneContactModel();
			model.setContactName(name);
			model.setContactNumber(phoneNumber);
			model.setAutoReply(false);
			// model.setAutoReplyMessage(autoReplyMessage);
			model.setLockCallPhone(false);
			model.setLockSMS(false);
			// Write Down Database
			databaseHelper.getPhoneContactExDao().createOrUpdate(model);

		}
		return false;

	}

	/** Update Auto Reply Field */
	public boolean UpdateOneFieldForListContact(String fieldName, List<PhoneContactModel> listPhone) {

		UpdateBuilder<PhoneContactModel, Long> updateBuilder = databaseHelper.getPhoneContactExDao().updateBuilder();
		try {

			for (PhoneContactModel phoneContactModel : listPhone) {
				updateBuilder.where().eq(PhoneContactTable.ID, phoneContactModel.getId());
				if (fieldName.equals(PhoneContactTable.LOCK_CALL_PHONE)) {
					updateBuilder.updateColumnValue(fieldName, phoneContactModel.isLockCallPhone());
				} else {
					updateBuilder.updateColumnValue(fieldName, phoneContactModel.isLockSMS());
				}
				updateBuilder.update();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;

	}

	public boolean UpdateReplyContact(List<PhoneContactModel> listPhones, String Message) {
		UpdateBuilder<PhoneContactModel, Long> updateBuilder = databaseHelper.getPhoneContactExDao().updateBuilder();
		try {

			for (PhoneContactModel phoneContactModel : listPhones) {
				updateBuilder.where().eq(PhoneContactTable.ID, phoneContactModel.getId());
				updateBuilder.updateColumnValue(PhoneContactTable.AUTO_REPLY, phoneContactModel.isAutoReply());
				updateBuilder.updateColumnValue(PhoneContactTable.AUTO_REPLY_MESSAGE, Message);
				updateBuilder.update();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * Query all contact with Block Phone Status*
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<PhoneContactModel> getBlockPhoneCallContactFollowByStatus(boolean status) throws SQLException {
		QueryBuilder<PhoneContactModel, Long> queryBuilder = databaseHelper.getPhoneContactExDao().queryBuilder();
		Where<PhoneContactModel, Long> where = queryBuilder.where().eq(PhoneContactTable.LOCK_CALL_PHONE, status);

		List<PhoneContactModel> contact = where.query();

		return contact;
	}

	/**
	 * Query all contact with Block Phone Status*
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<PhoneContactModel> getAutoreplyPhoneCallContactFollowByStatus(boolean status) throws SQLException {
		QueryBuilder<PhoneContactModel, Long> queryBuilder = databaseHelper.getPhoneContactExDao().queryBuilder();
		Where<PhoneContactModel, Long> where = queryBuilder.where().eq(PhoneContactTable.AUTO_REPLY, status);

		List<PhoneContactModel> contact = where.query();

		return contact;
	}

	/**
	 * Query all contact with Block SMS Status*
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<PhoneContactModel> getBlockPhoneSMSContactFollowByStatus(boolean status) throws SQLException {
		QueryBuilder<PhoneContactModel, Long> queryBuilder = databaseHelper.getPhoneContactExDao().queryBuilder();
		Where<PhoneContactModel, Long> where = queryBuilder.where().eq(PhoneContactTable.LOCK_SMS, status);

		List<PhoneContactModel> contact = where.query();

		return contact;
	}

	/**
	 * Query all Contact in db
	 * 
	 * @throws SQLException
	 */
	public List<PhoneContactModel> getPhoneContact() throws SQLException {
		QueryBuilder<PhoneContactModel, Long> queryBuilder = databaseHelper.getPhoneContactExDao().queryBuilder();
		List<PhoneContactModel> contact = queryBuilder.query();
		return contact;
	}

	/**
	 * Update All BLock Contact
	 * 
	 * @throws SQLException
	 */
	public boolean InsertSingleCustomContact(PhoneContactModel model, String CollumnUpdate) throws SQLException {
		QueryBuilder<PhoneContactModel, Long> queryBuilder = databaseHelper.getPhoneContactExDao().queryBuilder();
		Where<PhoneContactModel, Long> where = queryBuilder.where().eq(PhoneContactTable.CONTACT_NUMBER, model.getContactNumber());
		List<PhoneContactModel> data = where.query();
		if (data != null && data.size() > 0) {
			for (PhoneContactModel phoneContactModel : data) {
				if (phoneContactModel.getContactNumber() == model.getContactNumber()) {
					UpdateBuilder<PhoneContactModel, Long> updateBuilder = databaseHelper.getPhoneContactExDao().updateBuilder();
					updateBuilder.where().eq(PhoneContactTable.CONTACT_NUMBER, model.getContactNumber());
					if (CollumnUpdate.equals(PhoneContactTable.LOCK_CALL_PHONE)) {
						updateBuilder.updateColumnValue(CollumnUpdate, model.isLockCallPhone());
					} else {
						updateBuilder.updateColumnValue(CollumnUpdate, model.isLockSMS());
					}
				}
			}
		} else {
			databaseHelper.getPhoneContactExDao().create(model);
		}

		return true;

	}

	/**
	 * Add or Update Single Contact
	 */
	public boolean UpdatePhoneContacts(PhoneContactModel model) {
		try {
			UpdateBuilder<PhoneContactModel, Long> updateBuilder = databaseHelper.getPhoneContactExDao().updateBuilder();
			updateBuilder.where().eq(PhoneContactTable.CONTACT_NUMBER, model.getContactNumber());

			updateBuilder.updateColumnValue(PhoneContactTable.AUTO_REPLY_MESSAGE, model.getAutoReplyMessage());
			updateBuilder.update();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}
}
